/*
 * @Description: 
 * @Author: 47
 * @Date: 2023-01-03 15:38:49
 * @LastEditors: 47
 * @LastEditTime: 2023-02-18 16:01:00
 */

import { ref } from 'vue'
import { defineStore } from 'pinia'
import {
    reqGetMoreCourseList,
    reqUpdateCourseStatus,
    reqGetMyCourseList,
    reqAddMyCourse,
    reqGetCourseNoticeList,
    reqGetNoticeDetail,
    reqGetCourseDiscussList,
    reqGetDiscussDetail,
    reqGetCourseSignList,
    reqUpdateNoticeStatus,
    reqUpdateSignStatus,
    reqGetSignTime,
    reqGetCourseTestList,
    reqGetCourseHomeworkList,
    reqGetCourseHomeworkDetail,
    reqGetCourseTestDetail,
    reqGetChapterInfo,
    reqGetChapterDetail,
    reqUpdateDetailTaskPoint,
    reqUpdateChapterTaskPoint,

    reqGetCourseTaskPointNum,
    reqGetMyOkTaskNumTheCourse,
    reqGetSignSuccessTime,

    reqAddContent,
    reqToLike,
    reqUpload,
    reqAddComment,
    reqGetLikeList
} from "@/api/index"

export const useCourseStore = defineStore('course', {

    // 数据
    state: () => {
        return {
            //课程下的考试列表
            courseTestList: ref([]),
            //课程下的作业列表
            courseHomeworkList: ref([]),
            //作业详情对象
            homeworkDetailObj: ref({}),
            //考试详情对象
            testDetailObj: ref({}),
            //章节标题列表
            chapterInfo: ref([]),
            //章节详情
            chapterDetail: ref([]),
            // 自选课程列表
            courseList: ref([]),

            // 我的课程列表
            myCourseList: ref([]),

            // 我的课程id【我的课程跳转到课程信息时使用】
            courseId: ref(''),

            // 我的课程title【我的课程跳转到课程信息时使用】
            courseTitle: ref(''),

            // 通知列表的id【课程任务跳转到课程通知页面】
            noticeId: ref(''),

            // 课程信息
            courseMsg: ref(''),

            // 课程下的通知列表
            noticeList: ref([]),

            // 通知详情页信息以及对应的评论区
            noticeDetail: ref({}),

            // 课程的讨论列表
            discussList: ref([]),

            // 讨论详情页信息以及对应的评论区
            DetailInfo: ref({}),

            // 课程的签到列表
            SignList: ref([]),

            //课程任务点数量
            courseTaskPointNum:ref({}),
            //课程我完成的任务点数量
            myOkTasKNum:ref({}),

            // 签到成功时间、
            sianSuccessTime: ref(''),

            // 上传的文件地址
            uploadUrl: ref(''),

            // 地址
            addressInfo: ref(''),

            //喜欢列表
            likeList:ref([])
        }
    },

    // 方法
    actions: {
        async getCourseTestList(courseId,userId) {
            const result = await reqGetCourseTestList(courseId,userId)
            this.courseTestList = result.data
        },


        async getCourseHomeworkList(courseId,userId) {
            const result = await reqGetCourseHomeworkList(courseId,userId)
            this.courseHomeworkList = result.data
        },


        async getCourseHomeworkDetail(homeworkId) {
            const result = await reqGetCourseHomeworkDetail(homeworkId)
            this.homeworkDetailObj = result.data
        },

        async getCourseTestDetail(testId) {
            const result = await reqGetCourseTestDetail(testId)
            this.testDetailObj = result.data
        },

        async getChapterInfo(courseId, userId) {
            const result = await reqGetChapterInfo(courseId, userId)
            this.chapterInfo = result.data
        },

        async getChapterDetail(chapterId, userId) {
            const result = await reqGetChapterDetail(chapterId, userId)
            this.chapterDetail = result.data
        },

        async updateDetailTaskPoint(detailId, userId) {
            await reqUpdateDetailTaskPoint(detailId, userId)
        },
        async updateChapterTaskPoint(chapterId, userId) {
            await reqUpdateChapterTaskPoint(chapterId, userId)
        },
        // 自选课程列表
        async getMoreCourseList() {
            const result = await reqGetMoreCourseList()
            this.courseList = result.data
        },

        // 修改课程状态
        async updateCourseStatus(courseId) {
            await reqUpdateCourseStatus(courseId)
        },

        // 我的课程列表
        async getMyCourseList(userId, keyword) {
            const result = await reqGetMyCourseList(userId, keyword)
            this.myCourseList = result.data
        },

        // 将自选课程添加到我的课程
        async addMyCourse(params) {
            await reqAddMyCourse(params)
        },

        // 获取课程下的通知列表
        async getCourseNoticeList(courseId, userId) {
            const result = await reqGetCourseNoticeList(courseId, userId)
            this.noticeList = result.data
        },

        // 获取通知详情页信息以及对应的评论区
        async getNoticeDetail(noticeId,userId) {
            const result = await reqGetNoticeDetail(noticeId,userId)
            this.noticeDetail = result.data
        },

        // 获取课程的讨论列表
        async getCourseDiscussList(courseId,userId) {
            const result = await reqGetCourseDiscussList(courseId,userId)
            this.discussList = result.data
        },

        // 获取讨论详情页信息以及对应的评论区  
        async getDiscussDetail(discussId,userId) {
            const result = await reqGetDiscussDetail(discussId,userId)
            this.DetailInfo = result.data
        },

        //获取课程的签到列表 
        async getCourseSignList(courseId, userId) {
            const result = await reqGetCourseSignList(courseId, userId)
            this.SignList = result.data
        },

        //根据noticeId和userId更新通知状态 
        async updateNoticeStatus(noticeId, userId) {
            await reqUpdateNoticeStatus(noticeId, userId)
        },

        //根据signId和userId更新签到状态
        async updateSignStatus(signId, userId) {
            await reqUpdateSignStatus(signId, userId)
        },

        // 更新签到时间 
        async getSignTime(data) {
            await reqGetSignTime(data)
        },

        //根据courseId获取该课程下的任务点数量
        async getCourseTaskPointNum(courseId) {
            const result = await reqGetCourseTaskPointNum(courseId)
            this.courseTaskPointNum = result.data
        },
        //根据courseId获取该课程下的任务点数量
        async getMyOkTaskNumTheCourse(courseId,userId) {
            const result = await reqGetMyOkTaskNumTheCourse(courseId,userId)
            this.myOkTasKNum = result.data
        },
        
    
        // 获取签到成功时间
        async getSignSuccessTime(data) {
            const result = await reqGetSignSuccessTime(data)
            this.sianSuccessTime = result.data[0].sign_success_time
        },

        // 提交作业/考试内容
        async addContent(data) {
            await reqAddContent(data)
            // this.sianSuccessTime = result.data[0].sign_success_time
        },

        // 点赞
        async toLike(data) {
            await reqToLike(data)
        },
        
        // 上传文件 
        async upload(data) {
            const result = await reqUpload(data)
            this.uploadUrl = result.location
        },

        // 添加评论 
        async addComment(data) {
            await reqAddComment(data)
        },

        async getLikeList(id,judgeCode) {
            const result = await reqGetLikeList(id,judgeCode)
            this.likeList = result.data
        },
        



        
    },

    // 计算属性
    getters: {

        // 通知信息
        noticeMsg(state) {
            return state.noticeDetail.noticeMsg || {}
        },

        // 通知的评论区
        commentList(state) {
            return state.noticeDetail.commentList || []
        },

        // 讨论信息
        discussMsg(state) {
            return state.DetailInfo.discussMsg || {}
        },

        // 讨论的讨论区
        disList(state) {
            return state.DetailInfo.commentList || []
        }

    },

    // 开启数据缓存
    persist: {
        enabled: true,
        strategies: [{
            key: '',
            // storage:localStorage,
            paths: ['courseId', "courseTitle", "noticeId", "courseMsg", 'addressInfo']
        }]
    }

})